package cn.richard.demo.tax.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.richard.demo.tax.config.KieUtil;
import cn.richard.demo.tax.entity.DroolsRule;
import cn.richard.demo.tax.mapper.DroolsRuleMapper;
import cn.richard.demo.tax.service.DroolsRuleService;

@Service
public class DroolsRuleServiceImpl extends ServiceImpl<DroolsRuleMapper, DroolsRule> implements DroolsRuleService{
	@Value("${spring.profiles.active}")
	private String active;
	
	@Override
	public List<DroolsRule> listAll() {
		List<DroolsRule> list = new ArrayList<>();
		list.add(new DroolsRule(1, "重载规则测试", "package hello\r\n" + 
				"\r\n" + 
				"rule \"rule_hello_reload\"\r\n" + 
				"	when\r\n" + 
				"		eval(true)\r\n" + 
				"	then\r\n" + 
				"		System.out.println(\"eval(true) -> 我一直会被触发：\" + drools.getRule().getName());\r\n" + 
				"end"));
		return list;
	}
	
	@PostConstruct
	public void init() {
		KieUtil.setActive(active);
		KieUtil.setDroolsRuleService(this);
		//主动reload一次
		KieUtil.reload();
	}
}
